﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('CustomerInfo.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('CustomerInfo.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
      >删除</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="cCusCode">客户编码</a-select-option>
                <a-select-option key="cCusName">客户名称</a-select-option>
                <a-select-option key="cCusAbbName">客户简称</a-select-option>
                <a-select-option key="cCCCode">客户分类编码</a-select-option>
                <a-select-option key="cDCCode">地区编码</a-select-option>
                <a-select-option key="cTrade">所属行业</a-select-option>
                <a-select-option key="cCusAddress">地址</a-select-option>
                <a-select-option key="cCusPostCode">邮政编码</a-select-option>
                <a-select-option key="cCusRegCode">纳税人登记号</a-select-option>
                <a-select-option key="cCusBank">开户银行</a-select-option>
                <a-select-option key="cCusAccount">银行账号</a-select-option>
                <a-select-option key="cCusLPerson">法人</a-select-option>
                <a-select-option key="cCusEmail">Email地址</a-select-option>
                <a-select-option key="cCusPerson">联系人</a-select-option>
                <a-select-option key="cCusPhone">电话</a-select-option>
                <a-select-option key="cCusFax">传真</a-select-option>
                <a-select-option key="cCusHand">手机</a-select-option>
                <a-select-option key="cCusCreGrade">信用等级</a-select-option>
                <a-select-option key="cCusPayCond">付款条件编码</a-select-option>
                <a-select-option key="cCusOAddress">发货地址</a-select-option>
                <a-select-option key="cCusOType">发运方式编码</a-select-option>
                <a-select-option key="cCusHeadCode">客户总公司编码</a-select-option>
                <a-select-option key="CustomerKCode">客户级别</a-select-option>
                <a-select-option key="cProvince">省份</a-select-option>
                <a-select-option key="cCity">城市</a-select-option>
                <a-select-option key="cCounty">区县</a-select-option>
                <a-select-option key="District">地区;街道</a-select-option>
                <a-select-option key="ParentId">上级组织;父级ID</a-select-option>
                <a-select-option key="OriginRemark">来源备注</a-select-option>
                <a-select-option key="Remark">其他备注</a-select-option>
                <a-select-option key="IRInfo">开票要求</a-select-option>
                <a-select-option key="SaleType">销售性质;直销、渠道、代理</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{y:530}"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('CustomerInfo.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('CustomerInfo.Edit')&&hasPerm('CustomerInfo.Delete')" type="vertical" />
          <a v-if="hasPerm('CustomerInfo.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import moment from 'moment'
const columns = [
  { title: '更新时间', dataIndex: 'UpdateTime', width: '10%' },
  { title: '客户编码', dataIndex: 'cCusCode', width: '10%' },
  { title: '客户名称', dataIndex: 'cCusName', width: '10%' },
  { title: '客户简称', dataIndex: 'cCusAbbName', width: '10%' },
  { title: '客户分类编码', dataIndex: 'cCCCode', width: '10%' },
  { title: '地区编码', dataIndex: 'cDCCode', width: '10%' },
  { title: '所属行业', dataIndex: 'cTrade', width: '10%' },
  { title: '地址', dataIndex: 'cCusAddress', width: '10%' },
  { title: '邮政编码', dataIndex: 'cCusPostCode', width: '10%' },
  { title: '纳税人登记号', dataIndex: 'cCusRegCode', width: '10%' },
  { title: '开户银行', dataIndex: 'cCusBank', width: '10%' },
  { title: '银行账号', dataIndex: 'cCusAccount', width: '10%' },
  { title: '法人', dataIndex: 'cCusLPerson', width: '10%' },
  { title: 'Email地址', dataIndex: 'cCusEmail', width: '10%' },
  { title: '联系人', dataIndex: 'cCusPerson', width: '10%' },
  { title: '电话', dataIndex: 'cCusPhone', width: '10%' },
  { title: '传真', dataIndex: 'cCusFax', width: '10%' },
  { title: '手机', dataIndex: 'cCusHand', width: '10%' },
  { title: '扣率', dataIndex: 'iCusDisRate', width: '10%' },
  { title: '信用等级', dataIndex: 'cCusCreGrade', width: '10%' },
  { title: '信用额度', dataIndex: 'iCusCreLine', width: '10%' },
  { title: '信用期限', dataIndex: 'iCusCreDate', width: '10%' },
  { title: '付款条件编码', dataIndex: 'cCusPayCond', width: '10%' },
  { title: '发货地址', dataIndex: 'cCusOAddress', width: '10%' },
  { title: '发运方式编码', dataIndex: 'cCusOType', width: '10%' },
  { title: '客户总公司编码', dataIndex: 'cCusHeadCode', width: '10%' },
  { title: '应收余额', dataIndex: 'iARMoney', width: '10%' },
  { title: '最后交易日期', dataIndex: 'dLastDate', width: '10%' },
  { title: '停用日期', dataIndex: 'dEndDate', width: '10%' },
  { title: '是否控制信用额度', dataIndex: 'bCredit', width: '10%' },
  { title: '是否控制信用期限', dataIndex: 'bCreditDate', width: '10%' },
  { title: '客户级别', dataIndex: 'CustomerKCode', width: '10%' },
  { title: '税率%', dataIndex: 'iCusTaxRate', width: '10%' },
  { title: '分配给用户的时间', dataIndex: 'allot_user_time', width: '10%' },
  { title: '省份', dataIndex: 'cProvince', width: '10%' },
  { title: '城市', dataIndex: 'cCity', width: '10%' },
  { title: '区县', dataIndex: 'cCounty', width: '10%' },
  { title: '地区;街道', dataIndex: 'District', width: '10%' },
  { title: '上级组织;父级ID', dataIndex: 'ParentId', width: '10%' },
  { title: '分配状态 0 未分配 1 已分配', dataIndex: 'AllocationStatus', width: '10%' },
  { title: '来源备注', dataIndex: 'OriginRemark', width: '10%' },
  { title: '其他备注', dataIndex: 'Remark', width: '10%' },
  { title: '客户类型;0 潜在  1 正式   2 休眠', dataIndex: 'cCusType', width: '10%' },
  { title: '开票要求', dataIndex: 'IRInfo', width: '10%' },
  { title: '销售性质;直销、渠道、代理', dataIndex: 'SaleType', width: '10%' },
  { title: '创建时间', dataIndex: 'CreateTime', width: '10%', scopedSlots: { customRender: 'CreateTime' } },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/CustomerManagement/CustomerInfo/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建客户信息表信息')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑客户信息表信息')
    },
    handleDelete (ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
       return new Promise((resolve, reject) => {
          thisObj.$http.post('/CustomerManagement/CustomerInfo/DeleteData', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat (val) { // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
